package service.impl.security;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.impl.BaseDataServiceImpl;
import service.interf.security.IGroupMenu;
import common.impl.exception.ServiceException;
import dao.interf.IGroupDAO;
import dao.interf.IGroupMenuDAO;
import dao.interf.IMenuDAO;
import dao.pojo.Group;
import dao.pojo.GroupMenu;
import dao.pojo.Menu;

public class GroupMenuImpl extends BaseDataServiceImpl implements IGroupMenu{
	private IGroupMenuDAO groupMenuDAO;
	private IGroupDAO groupDAO;
	private IMenuDAO menuDAO;
	
	public IMenuDAO getMenuDAO() {
		return menuDAO;
	}

	public void setMenuDAO(IMenuDAO menuDAO) {
		this.menuDAO = menuDAO;
	}

	public IGroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(IGroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public IGroupMenuDAO getGroupMenuDAO() {
		return groupMenuDAO;
	}

	public void setGroupMenuDAO(IGroupMenuDAO groupMenuDAO) {
		this.groupMenuDAO = groupMenuDAO;
	}
	//添加groupMenu
	public String addGroupMenu(Integer groupId,String menuIds) throws Exception{
		Group group = groupDAO.findById(groupId);
		if(group == null){
			throw new ServiceException("ERR-0011");
		}
		//删除group的所有menu
		groupMenuDAO.deleteByProperties(new String[]{"group"}, new Object[]{group}, null);
		String[] menuArr = menuIds.split(",");
		if(!menuArr[0].equals("")){
			for(int i = 0; i < menuArr.length;i++){
				Menu menu = null;
				menu = menuDAO.findById(new Integer(menuArr[i]));
				if(menu == null){
					throw new ServiceException("ERR-0010");
				}
				GroupMenu groupMenu = new GroupMenu();
				groupMenu.setGroup(group);
				groupMenu.setMenu(menu);
				//保存
				groupMenuDAO.save(groupMenu);
			}
		}
		return "1";
	}
	//移除groupMenu
	public String removeGroupMenu(Integer groupId,String menuIds) throws Exception{
		Group group = groupDAO.findById(groupId);
		String[] menuArr = menuIds.split(",");
		for(int i = 0; i < menuArr.length;i++){
			Menu menu = null;
			menu = menuDAO.findById(new Integer(menuArr[i]));
			if(menu == null){
				throw new ServiceException("ERR-0010");
			}
			groupMenuDAO.deleteByProperties(new String[]{"group","menu"}, new Object[]{group,menu}, null);
		}
		return "1";
	}
	
	//根据GroupId获取该group拥有的menu
	public String getMenuByGroupId(Integer groupId) throws Exception{
		Group group = groupDAO.findById(groupId);
		if(group == null){
			throw new ServiceException("ERR-0011");
		}
		List<GroupMenu> menuGroupList = groupMenuDAO.findByProperties(new String[]{"group"}, new Object[]{group});
		JSONObject jobj = new JSONObject();
		JSONArray jarr = new JSONArray();
		if(menuGroupList != null){
			for(GroupMenu groupMenu : menuGroupList){
				Menu menu = groupMenu.getMenu();
				JSONObject jo = new JSONObject();
				jo.put("menuId", menu.getMenuId());
				jo.put("title", menu.getTitle());
				jarr.add(jo);
			}
		}
		jobj.put("rows", jarr);
		return jobj.toString();
	}
	//为某一个菜单添加组
	public String addMenuGroup(Integer menuId,String groupIds) throws Exception{
		Menu menu = menuDAO.findById(menuId);
		if(menu == null){
			throw new ServiceException("ERR-0010");
		}
		groupMenuDAO.deleteByProperties(new String[]{"menu"}, new Object[]{menu}, null);
		String[] groupArr = groupIds.split(",");
		if(!groupArr[0].equals("")){
			for(int i = 0;i<groupArr.length;i++){
				Group group = null;
				group = groupDAO.findById(new Integer(groupArr[i]));
				if(null == group){
					throw new ServiceException("ERR-0011");
				}
				GroupMenu groupMenu = new GroupMenu();
				groupMenu.setMenu(menu);
				groupMenu.setGroup(group);
				groupMenuDAO.save(groupMenu);
			}
		}
		return "1";
	}
}
